package com.example.im;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;

import com.example.adapter.AddFriendListAdapter;
import com.example.model.Friends;
import com.example.model.User;
import com.example.util.BaseActivity;
import com.example.util.MyUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import static com.example.db.CommonData.commonData;
import static com.example.db.CommonData.getDbFriendList;
import static com.example.db.CommonData.getDbUserList;

public class AddFriendsActivity extends BaseActivity {
    private String[] mStrs = {""};
    private SearchView search;
    private ListView search_list;
    private ImageView back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friends);
        back=findViewById(R.id.back);
        searchViewInit();
//        ((AddFriendListAdapter) search_list.getAdapter()).flush(search.getQuery().toString());
        back.setOnClickListener(v -> {
            MyUtil.changeActivity(this,MainActivity.class);
        });
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//
//        if (search_list != null && search_list.getAdapter() != null && search != null) {
//            ((AddFriendListAdapter) search_list.getAdapter()).flush(search.getQuery().toString());
//        }
//
//    }

    private void searchViewInit() {
        search=findViewById(R.id.search);
        search_list=findViewById(R.id.search_list);

        ArrayList<User> adapterUserList = new ArrayList<>();
        search_list.setAdapter(new AddFriendListAdapter(this, adapterUserList));

        // 设置搜索文本监听
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                //
                ((AddFriendListAdapter) search_list.getAdapter()).flush(query);
                return false;
            }
            /**
             *
             * @param newText
             * 当搜索内容改变时触发该方法
             * @return
             */
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    search_list.setFilterText(newText);
                } else{
                    search_list.clearTextFilter();
                }
                return false;
            }
        });
    }

}
